package trinidad;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EtchedBorder;



public class NewGameWindow extends JFrame implements ActionListener, ItemListener {
	
	private static final long serialVersionUID = 1L;
	static String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
    protected JPanel mainWindow;
    private NeTrinidadApp parent;
  
    private JComboBox bluePlayerList, violetPlayerList;
	private JComboBox whoStartsList;
	private JComboBox dificultyLevelList;
	private JComboBox algorithmList;
	private JTextField searchingDepthTextField;
	private JTextField ttAmnesiaTextField;
	private JPanel leftTopPanel;
	private JPanel leftBottomPanel;
	private JPanel rightPanel;
    
    /*public static void main(String[] args)
    {
    	NewGameWindow n = new NewGameWindow(null);
    }*/

    public NewGameWindow(NeTrinidadApp parent) {
    	super("Nowa gra");
    	this.parent = parent;
		this.setSize(300, 280);
		this.setResizable(false);

		try {
			UIManager.setLookAndFeel(windows);
		} catch (UnsupportedLookAndFeelException exc) {
		} catch (IllegalAccessException exc) {
		} catch (ClassNotFoundException exc) {
		} catch (InstantiationException exc) {
		}
		mainWindow = setMainWindow();
		getContentPane().add(mainWindow , BorderLayout.CENTER);
		this.setVisible(true);
	    this.setLocationRelativeTo(parent.winMain);
    }
    
	protected JPanel setMainWindow() {
		JPanel mainPanel = new JPanel();
		JPanel settingsPanel = new JPanel();
		JPanel buttonsPanel = new JPanel();
		JPanel leftOuterPanel = new JPanel();
		JPanel rightOuterPanel = new JPanel();
		leftTopPanel = new JPanel();
		leftBottomPanel = new JPanel();
		rightPanel = new JPanel();
		
		leftTopPanel.setMaximumSize(new Dimension(150,140));
		leftBottomPanel.setMaximumSize(new Dimension(150,58));
		rightPanel.setMaximumSize(new Dimension(150,198));
		
    
  		leftTopPanel.setBorder(BorderFactory.createCompoundBorder(
               BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
               BorderFactory.createEmptyBorder(5,5,5,5)  ));
  		leftTopPanel.setLayout(new GridLayout(6,1));
  		String[] playersStrings = { "Człowiek", "Komputer" };
  		
		JLabel bluePlayerLabel = new JLabel("Gracz niebieski");
		leftTopPanel.add(bluePlayerLabel);
        bluePlayerList = new JComboBox(playersStrings);
        bluePlayerList.setName("bluePlayerList");
        bluePlayerList.setSelectedIndex(0);
        bluePlayerList.addItemListener(this);
        leftTopPanel.add(bluePlayerList);
        
        JLabel violetPlayerLabel = new JLabel("Gracz fioletowy");
        leftTopPanel.add(violetPlayerLabel);
		violetPlayerList = new JComboBox(playersStrings);
		violetPlayerList.setName("violetPlayerList");
		violetPlayerList.setSelectedIndex(0);
		violetPlayerList.addItemListener(this);
		leftTopPanel.add(violetPlayerList);
		
		JLabel whoStartsLabel = new JLabel("Kto zaczyna");
		leftTopPanel.add(whoStartsLabel);
		whoStartsList = new JComboBox(new String[] {"niebieski", "fioletowy"} );
		whoStartsList.setSelectedIndex(0);
        leftTopPanel.add(whoStartsList);
        
		/*JButton playersButton = new JButton("Ok");
		button.setActionCommand("OK");
		button.addActionListener(this);
		JPanel jp=new JPanel();
		jp.add(button);
		leftTopPanel.add(jp);*/
		
  		leftBottomPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
                BorderFactory.createEmptyBorder(5,5,5,5)  ));
   		leftBottomPanel.setLayout(new GridLayout(2,1));

   		JLabel dificultyLevel = new JLabel("Poziom trudności");
   		leftBottomPanel.add(dificultyLevel);
   		dificultyLevelList = new JComboBox(new String[] {"łatwy", "średni", "trudny", "niestandardowy"} );
   		dificultyLevelList.setName("dificultyLevelList");
   		dificultyLevelList.setSelectedIndex(0);
   		dificultyLevelList.addItemListener(this);
   		leftBottomPanel.add(dificultyLevelList);
   		this.setEnabled(false,leftBottomPanel.getComponents());
   		
   		leftOuterPanel.setLayout(new BoxLayout(leftOuterPanel,BoxLayout.PAGE_AXIS));//new GridLayout(2,1));
   		leftOuterPanel.add(leftTopPanel);
   		leftOuterPanel.add(leftBottomPanel);
   		
  		rightPanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createEtchedBorder(EtchedBorder.LOWERED),
                BorderFactory.createEmptyBorder(5,5,5,5)  ));
   		rightPanel.setLayout(new GridLayout(9,1));

		JLabel algorithmLabel = new JLabel("Algorytm");
		rightPanel.add(algorithmLabel);
        algorithmList = new JComboBox(new String[] {"Alfa Killer", "Power-Beta", "UnderTTaker", "Maddog MTD", "Emptiness Murderess"} );
        algorithmList.setName("algorithmList");
        algorithmList.setSelectedIndex(0);
        algorithmList.addItemListener(this);
        rightPanel.add(algorithmList);
        
        JLabel searchingDepthLabel = new JLabel("Głębokość przeszukiwania");
        rightPanel.add(searchingDepthLabel);
        searchingDepthTextField = new JTextField(20);
        searchingDepthTextField.setText("2");
		rightPanel.add(searchingDepthTextField);
		
		JLabel ttAmnesiaLabel = new JLabel("TT amnestia");
		rightPanel.add(ttAmnesiaLabel);
		ttAmnesiaTextField = new JTextField(20);
		ttAmnesiaTextField.setText("50000");
		rightPanel.add(ttAmnesiaTextField);
   		
		this.setEnabled(false, rightPanel.getComponents());
		
   		rightOuterPanel.setLayout(new BoxLayout(rightOuterPanel,BoxLayout.Y_AXIS));
   		rightOuterPanel.add(rightPanel);

   		settingsPanel.setLayout(new GridLayout(1,2));
   		settingsPanel.add(leftOuterPanel);
   		settingsPanel.add(rightOuterPanel);
   		
   		buttonsPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
   		buttonsPanel.setLayout(new BoxLayout(buttonsPanel,BoxLayout.X_AXIS));
   		
   		JButton cancelButton = new JButton("Anuluj");
   		cancelButton.addActionListener(this);
   		cancelButton.setActionCommand("cancelButton");
   		JButton newGameButton = new JButton("Nowa gra");
   		newGameButton.addActionListener(this);
   		newGameButton.setActionCommand("newGameButton");
   		buttonsPanel.add(cancelButton);
   		buttonsPanel.add(newGameButton);
   		
   		mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
   		mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.PAGE_AXIS));
   		mainPanel.add(settingsPanel);
   		mainPanel.add(buttonsPanel);
		mainPanel.setOpaque(true);
		return mainPanel;
	}
    

	private void setEnabled(boolean state, Component[] components)
	{
   		for(int i = 0 ; i < components.length ; i++)
   		{
   			components[i].setEnabled(state);
   		}	
	}
    
	public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        if (cmd.equals("cancelButton")) {
        	this.dispose();
        }
        else if(cmd.equals("newGameButton")) {
           	int maxdepth = Integer.parseInt(this.searchingDepthTextField.getText());
        	if (maxdepth <= 0) {
        		maxdepth = 4;
        	}
        	
        	int overrun = Integer.parseInt(this.ttAmnesiaTextField.getText());
        	if (overrun <= 0) {
        		overrun = 100000;
        	}
        	
        	String opponent = "human";
        	if (this.dificultyLevelList.isEnabled()) {
        		String opponentBuf = (String) this.algorithmList.getSelectedItem();
        		if (opponentBuf.equals("Alfa Killer")) {
        			opponent = "alfazabojca";
        		} else if (opponentBuf.equals("Power-Beta")) {
        			opponent = "powerbeta";
        		} else if (opponentBuf.equals("UnderTTaker")) {
        			opponent = "underttaker";
        		} else if (opponentBuf.equals("Maddog MTD")) {
        			opponent = "maddogmtd";
        		} else if (opponentBuf.equals("Emptiness Murderess")) {
        			opponent = "emptinessmurderess";
        		}
        	}
        	
        	boolean reverseturns = false;
        	if (((String)this.whoStartsList.getSelectedItem()).equals("fioletowy")) {
        		reverseturns = true;
        	}
        	
        	boolean humanfirst = true;
        	if ( (this.whoStartsList.getSelectedItem().equals("niebieski")
        		&& this.bluePlayerList.getSelectedItem().equals("Komputer"))
        		|| (this.whoStartsList.getSelectedItem().equals("fioletowy")
                && this.violetPlayerList.getSelectedItem().equals("Komputer"))       	
        	) {
        		humanfirst = false;
        	}
			this.parent.setParameters(maxdepth, overrun, opponent, reverseturns, humanfirst);
			this.parent.setNetParams(null,null,false);
			this.parent.newGame();
			this.dispose();
		}
	}

	public void itemStateChanged(ItemEvent e) {
		JComboBox comboBox = (JComboBox) e.getSource();
		
		if (comboBox.getName().equals("bluePlayerList")) {
			if ( ((String)bluePlayerList.getSelectedItem()).equals("Komputer") ) {
				violetPlayerList.setSelectedItem("Człowiek");
				this.setEnabled(true, leftBottomPanel.getComponents());
				if ( ((String)dificultyLevelList.getSelectedItem()).equals("niestandardowy") ) {
					this.setEnabled(true, this.rightPanel.getComponents());
				}
			}
			else if (  ((String)bluePlayerList.getSelectedItem()).equals("Człowiek")
					&& ((String)violetPlayerList.getSelectedItem()).equals("Człowiek") ) {
				this.setEnabled(false, this.leftBottomPanel.getComponents());
				this.setEnabled(false, this.rightPanel.getComponents());
			}
		}
		else if (comboBox.getName().equals("violetPlayerList")) {
			if ( ((String)violetPlayerList.getSelectedItem()).equals("Komputer") ) {
				bluePlayerList.setSelectedItem("Człowiek");
				this.setEnabled(true, leftBottomPanel.getComponents());
				if ( ((String)dificultyLevelList.getSelectedItem()).equals("niestandardowy") ) {
					this.setEnabled(true, this.rightPanel.getComponents());
				}
			}
			else if (  ((String)bluePlayerList.getSelectedItem()).equals("Człowiek")
					&& ((String)violetPlayerList.getSelectedItem()).equals("Człowiek") ) {
				this.setEnabled(false, this.leftBottomPanel.getComponents());
				this.setEnabled(false, this.rightPanel.getComponents());
			}
		}
		else if (comboBox.getName().equals("dificultyLevelList")) {
			String selectedItem = (String)dificultyLevelList.getSelectedItem();
			
			if (selectedItem.equals("łatwy")) {
				this.algorithmList.setSelectedItem("Alfa Killer");
				this.searchingDepthTextField.setText("2");
				this.ttAmnesiaTextField.setText("50000");
				this.setEnabled(false, this.rightPanel.getComponents());
			} else if (selectedItem.equals("średni")) {
				this.algorithmList.setSelectedItem("Underttaker");
				this.searchingDepthTextField.setText("4");
				this.ttAmnesiaTextField.setText("100000");
				this.setEnabled(false, this.rightPanel.getComponents());
			} else if (selectedItem.equals("trudny")) {
				this.algorithmList.setSelectedItem("Maddog MTD");
				this.searchingDepthTextField.setText("6");
				this.ttAmnesiaTextField.setText("300000");
				this.setEnabled(false, this.rightPanel.getComponents());
			} else if (selectedItem.equals("niestandardowy")) {
				this.setEnabled(true, this.rightPanel.getComponents());
			}
		}
	}
}
